package cn.com.inossem.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.util.Base64;
import android.view.Window;
import android.view.WindowManager;

import cn.com.inossem.systembartint.SystemBarTintManager;
import cn.com.inossem.utils.AsyncImageLoader;
import cn.com.inossem.utils.AsyncImageLoader.ImageCallback;
import cn.com.inossem.utils.Utils;
import cn.com.inossem.utils.view.MatrixImageView;

public class ImageViewDialog extends Activity {

	public MatrixImageView imageview;
	String path;
	int type = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setTintColor(getResources().getColor(R.color.app_main));

		setContentView(R.layout.imageviewdialog);

		path = getIntent().getStringExtra("path").replace("s_", "");

		type = getIntent().getIntExtra("type", 0);

		imageview = (MatrixImageView) this.findViewById(R.id.imageview);
		if (type == 0) {
			AsyncImageLoader asyncImageLoader = new AsyncImageLoader(this);
			Drawable drawable = asyncImageLoader.loadDrawable(path,
					new ImageCallback() {
						@Override
						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							if (imageDrawable != null) {

								imageview.setImageBitmap(Utils
										.drawableToBitmap(imageDrawable));
							}
						}
					});
			if (drawable != null) {
				imageview.setImageBitmap(Utils.drawableToBitmap(drawable));
			}
		} else if (type == 1) {
			imageview.setBackgroundDrawable(new BitmapDrawable(
					stringtoBitmap(path)));
		}
	}

	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
}
